package org.now.ypos.user.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.now.ypos.user.dto.UserDto;
import org.now.ypos.user.dto.UserStruct;
import org.now.ypos.user.entity.User;
import org.now.ypos.user.mapper.UserMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * User Service impl
 *
 * @author wl
 * @since 2020.11.10
 */
@Service
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    public UserServiceImpl(final UserMapper userMapper) {
        this.userMapper = userMapper;

    }

    @Override
    public final User save(final UserDto userDto) {
        final User user = UserStruct.INSTANCE.toEntity(userDto);
        userMapper.insert(user);
        return user;
    }

    @Override
    public final User update(final UserDto userDto) {
        final User user = UserStruct.INSTANCE.toEntity(userDto);
        userMapper.updateById(user);
        return getById(user.getId());
    }

    @Override
    public final User getById(final Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public final int deleteById(final Long id) {
        return userMapper.deleteById(id);
    }

    @Override
    public final List<User> find(final UserDto userDto) {
        return userMapper.selectList(Wrappers.query(UserStruct.INSTANCE.toEntity(userDto)));
    }

    @Override
    public final IPage<User> findPage(final UserDto userDto, final IPage<User> page) {
        return userMapper.selectPage(page, Wrappers.query(UserStruct.INSTANCE.toEntity(userDto)));
    }
}
